package com.example.demo.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import com.example.demo.captcha.MyCodeGenerator;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

/**
 * @author 何景航
 * @date 2025/3/7 10:45
 */
@Controller
public class CaptchaController {
    @RequestMapping(value = "/common/captcha")
    public void generateCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException{
       response.setContentType("image/jpeg");
//       ICaptcha captcha = CaptchaUtil.createCircleCaptcha(90,30,4,10);
//       ICaptcha captcha = CaptchaUtil.createCircleCaptcha(90,30,new MyCodeGenerator(),10);
       ICaptcha captcha = CaptchaUtil.createGifCaptcha(90,30,new MyCodeGenerator(),10);
//       ICaptcha captcha = CaptchaUtil.createGifCaptcha(90,30,4,10,1);
//        ICaptcha captcha = CaptchaUtil.createLineCaptcha(90,30,4,10,1);
//        ICaptcha captcha = CaptchaUtil.createShearCaptcha(90,30,4,2,1);
        request.getSession().setAttribute("captcha",captcha.getCode());
       captcha.write(response.getOutputStream());
    }
}










